<template>
  <div>
    <el-form ref="addFormRef" :rules="rules" :model="addForm.data" label-width="120">
      <el-form-item label="班次名称" prop="name">
        <el-input
          v-model="addForm.data.name"
          style="width: 320px"
          placeholder="请输入班次名称"
        />
      </el-form-item>
      <el-form-item label="上班时间" prop="startTime">
        <el-time-select
          v-model="addForm.data.startTime"
          style="width: 320px"
          :max-time="addForm.data.endTime"
          start="00:00"
          step="00:15"
          end="24:00"
          placeholder="请选择开始时间"
        />
      </el-form-item>
      <el-form-item label="下班时间" prop="endTime">
        <el-time-select
          v-model="addForm.data.endTime"
          style="width: 320px"
          :min-time="addForm.data.startTime"
          start="00:00"
          step="00:15"
          end="24:00"
          placeholder="请选择结束时间"
        />
      </el-form-item>
      <el-form-item label="代表颜色" prop="markColor">
        <el-select
          v-model="addForm.data.markColor"
          style="width: 320px"
          placeholder="请选择代表颜色"
        >
          <el-option
            v-for="item in fontColor.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
            <div class="flex items-center">
              <el-tag :color="item.id" style="margin-right: 8px" size="small" />
              <span :style="{ color: item.id }">{{ item.name }}</span>
            </div>
          </el-option>
          <template #tag>
            <el-tag :color="addForm.data.markColor" />
          </template>
        </el-select>
      </el-form-item>
      <!-- <div
        :style="{
          color: addForm.data.markColor,
        }"
      >
        1
      </div> -->
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(addFormRef)">重 置</el-button>
            <el-button class="confirm-btn" @click="submitForm(addFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { getAddShift } from "../request.js";
import { getDictionary } from "@/service/modules/publicModule";

const addFormRef = ref("");

const addForm = reactive({
  data: {
    name: "",
    startTime: "",
    endTime: "",
    markColor: "",
  },
});
const fontColor = reactive({
  data: [],
});

const rules = reactive({
  name: [{ required: true, message: "请输入班次名称", trigger: "blur" }],
  startTime: [{ required: true, message: "请选择收费开始时间", trigger: "change" }],
  endTime: [{ required: true, message: "请选择收费结束时间", trigger: "change" }],
  markColor: [{ required: true, message: "请选择班次颜色", trigger: "change" }],
});

const getData = async () => {
  await getDictionary("shift_mark_color").then((res) => {
    fontColor.data = res.data.result;
  });
};

getData();

const resetForm = (formEl) => {
  if (!formEl) return;
  addForm.data = {};
  formEl.resetFields();
};

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      // console.log("submit!");
      getAddShift(addForm.data).then((res) => {
        // console.log("res ===>", res);
        emits("operationDone");
      });
    }
  });
};

const emits = defineEmits(["operationDone"]);
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
.el-tag {
  border: none;
  aspect-ratio: 1;
}
</style>
